import { getPlayListComment, getHotComment } from "@/api/comment";
import { Comment } from "@/types/comment";
import { ref } from "vue";

export function usePlayListComment() {
  const playlistCommentData = ref<{
    cnum: number;
    code: number;
    commentBanner: any;
    comments: Comment[];
    hotComments: Comment[];
    isMusician: boolean;
    more: boolean;
    topComments: Comment[];
    total: number;
    userId: number;
  } | null>(null);

  const queryPlaylistComment = (
    params: {
      id: number | string;
      limit: number;
      offset: number;
    },
    onsuccess?: () => void
  ) => {
    getPlayListComment(params).then((res) => {
      console.log(res);
      if (res.code === 200) {
        try {
          typeof onsuccess === "function" && onsuccess();
        } catch (err) {}
        playlistCommentData.value = res;
      }
    });
  };

  return { playlistCommentData, queryPlaylistComment };
}

export function useHotComments() {
  const hotComments = ref<Comment[]>([]);
  const total = ref<number>(0);

  const queryHotComments = (params: {
    limit?: number;
    offset?: number;
    before?: number;
    id: number | string;
    type: number;
  }) => {
    getHotComment(params).then((res) => {
      if (res.code === 200) {
        hotComments.value = res.hotComments;
        total.value = res.total;
      }
    });
  };

  return { hotComments, total, queryHotComments };
}
